import { assign } from 'min-dash';

export class PaletteProvider {
  static $inject = [
    'palette',
    'create',
    'elementFactory',
    'spaceTool',
    'lassoTool',
    'handTool',
    'globalConnect',
    'translate'
  ];

  private palette: any;
  private create: any;
  private elementFactory: any;
  private spaceTool: any;
  private lassoTool: any;
  private handTool: any;
  private globalConnect: any;
  private translate: any;

  constructor(
    palette: any,
    create: any,
    elementFactory: any,
    spaceTool: any,
    lassoTool: any,
    handTool: any,
    globalConnect: any,
    translate: any
  ) {
    this.palette = palette;
    this.create = create;
    this.elementFactory = elementFactory;
    this.spaceTool = spaceTool;
    this.lassoTool = lassoTool;
    this.handTool = handTool;
    this.globalConnect = globalConnect;
    this.translate = translate;

    palette.registerProvider(this);
  }

  getPaletteEntries() {
    const {
      create,
      elementFactory,
      spaceTool,
      lassoTool,
      handTool,
      globalConnect,
      translate
    } = this;

    function createAction(type: string, group: string, className: string, title: string, options?: any) {
      function createListener(event: MouseEvent) {
        const shape = elementFactory.createShape(assign({ type }, options));
        if (options) {
          shape.businessObject.di.isExpanded = options.isExpanded;
        }
        create.start(event, shape);
      }

      const shortType = type.replace(/^bpmn:/, '');

      return {
        group,
        className,
        title: translate(title || shortType),
        action: {
          dragstart: createListener,
          click: createListener
        }
      };
    }

    return {
      'hand-tool': {
        group: 'tools',
        className: 'bpmn-icon-hand-tool',
        title: translate('Activate the hand tool'),
        action: {
          click: function(event: MouseEvent) {
            handTool.activateHand(event);
          }
        }
      },
      'lasso-tool': {
        group: 'tools',
        className: 'bpmn-icon-lasso-tool',
        title: translate('Activate the lasso tool'),
        action: {
          click: function(event: MouseEvent) {
            lassoTool.activateSelection(event);
          }
        }
      },
      'space-tool': {
        group: 'tools',
        className: 'bpmn-icon-space-tool',
        title: translate('Activate the create/remove space tool'),
        action: {
          click: function(event: MouseEvent) {
            spaceTool.activateSelection(event);
          }
        }
      },
      'global-connect-tool': {
        group: 'tools',
        className: 'bpmn-icon-connection-multi',
        title: translate('Activate the global connect tool'),
        action: {
          click: function(event: MouseEvent) {
            globalConnect.toggle(event);
          }
        }
      },
      'tool-separator': {
        group: 'tools',
        separator: true
      },
      'create.start-event': createAction(
        'bpmn:StartEvent',
        'event',
        'bpmn-icon-start-event-none',
        'Create StartEvent'
      ),
      'create.intermediate-event': createAction(
        'bpmn:IntermediateThrowEvent',
        'event',
        'bpmn-icon-intermediate-event-none',
        'Create Intermediate/Boundary Event'
      ),
      'create.end-event': createAction(
        'bpmn:EndEvent',
        'event',
        'bpmn-icon-end-event-none',
        'Create EndEvent'
      ),
      'create.exclusive-gateway': createAction(
        'bpmn:ExclusiveGateway',
        'gateway',
        'bpmn-icon-gateway-none',
        'Create Gateway'
      ),
      'create.task': createAction(
        'bpmn:Task',
        'activity',
        'bpmn-icon-task',
        'Create Task'
      ),
      'create.user-task': createAction(
        'bpmn:UserTask',
        'activity',
        'bpmn-icon-user-task',
        'Create User Task'
      ),
      'create.service-task': createAction(
        'bpmn:ServiceTask',
        'activity',
        'bpmn-icon-service-task',
        'Create Service Task'
      ),
      'create.script-task': createAction(
        'bpmn:ScriptTask',
        'activity',
        'bpmn-icon-script-task',
        'Create Script Task'
      ),
      'create.business-rule-task': createAction(
        'bpmn:BusinessRuleTask',
        'activity',
        'bpmn-icon-business-rule-task',
        'Create Business Rule Task'
      ),
      'create.receive-task': createAction(
        'bpmn:ReceiveTask',
        'activity',
        'bpmn-icon-receive-task',
        'Create Receive Task'
      ),
      'create.manual-task': createAction(
        'bpmn:ManualTask',
        'activity',
        'bpmn-icon-manual-task',
        'Create Manual Task'
      ),
      'create.send-task': createAction(
        'bpmn:SendTask',
        'activity',
        'bpmn-icon-send-task',
        'Create Send Task'
      ),
      'create.subprocess-expanded': createAction(
        'bpmn:SubProcess',
        'activity',
        'bpmn-icon-subprocess-expanded',
        'Create Expanded SubProcess',
        { isExpanded: true }
      ),
      'create.participant-expanded': createAction(
        'bpmn:Participant',
        'collaboration',
        'bpmn-icon-participant',
        'Create Pool/Participant',
        { isExpanded: true }
      )
    };
  }
} 